package com.netbetter.langzheng.plugin.fileManager.widget;

import java.util.ArrayList;
import java.util.List;

import com.extjs.gxt.ui.client.Style.SelectionMode;
import com.extjs.gxt.ui.client.binder.TableBinder;
import com.extjs.gxt.ui.client.data.BaseListLoader;
import com.extjs.gxt.ui.client.data.LoadEvent;
import com.extjs.gxt.ui.client.event.SelectionChangedEvent;
import com.extjs.gxt.ui.client.event.SelectionChangedListener;
import com.extjs.gxt.ui.client.mvc.AppEvent;
import com.extjs.gxt.ui.client.mvc.Dispatcher;
import com.extjs.gxt.ui.client.store.ListStore;
import com.extjs.gxt.ui.client.widget.ContentPanel;
import com.extjs.gxt.ui.client.widget.layout.FitLayout;
import com.extjs.gxt.ui.client.widget.table.Table;
import com.extjs.gxt.ui.client.widget.table.TableColumn;
import com.extjs.gxt.ui.client.widget.table.TableColumnModel;
import com.netbetter.langzheng.commons.constants.AppEvents;
import com.netbetter.langzheng.plugin.fileManager.context.ChangeListItemEventContext;
import com.netbetter.langzheng.plugin.fileManager.context.ChangeTreeItemEventContext;
import com.netbetter.langzheng.plugin.fileManager.context.TableEventContext;
import com.netbetter.langzheng.plugin.fileManager.data.FileManagerRequest;
import com.netbetter.langzheng.plugin.fileManager.dataProxy.FileModelDataProxy;
import com.netbetter.langzheng.plugin.fileManager.model.FileModel;
import com.netbetter.langzheng.plugin.fileManager.reader.FileInfoReader;
import com.netbetter.langzheng.plugin.fileManager.widget.table.ListViewTable;
import com.netbetter.langzheng.plugin.fileManager.widget.toolbar.FileToolbar;

/**
 * ListView
 * 
 * @author 嘉陵江里捉鱼
 */
public class ListViewPanel extends ContentPanel {

    private FileToolbar fileToolbar;
    private Table       table;

    public ListViewPanel() {
        this.setHeaderVisible(false);
        List<TableColumn> columns = new ArrayList<TableColumn>();
        TableColumn idColumn = new TableColumn("id", "id", 0f);
        idColumn.setHidden(true);
        columns.add(idColumn);
        columns.add(new TableColumn("iconName", "名称", .2f));
        columns.add(new TableColumn("fSize", "大小", .2f));
        columns.add(new TableColumn("type", "类型", .2f));
        columns.add(new TableColumn("createTime", "创建时间", .2f));
        TableColumnModel cm = new TableColumnModel(columns);

        table = new ListViewTable(cm);
        table.setSelectionMode(SelectionMode.MULTI);
        table.setBorders(false);
        add(table);

        setLayout(new FitLayout());

        fileToolbar = new FileToolbar();
        this.setTopComponent(fileToolbar);

        Dispatcher.get().addController(new BaseEventCenter() {

            @Override
            public void handleEvent(AppEvent<?> event) {
                if (event.type == AppEvents.ChangeTreeItem) {
                    FileModel fileModel = ChangeTreeItemEventContext.getFileModel();
                    FileModelDataProxy proxy = new FileModelDataProxy(FileManagerRequest.getFileInfo(fileModel));
                    @SuppressWarnings("unchecked")
                    BaseListLoader loader = new BaseListLoader(proxy, new FileInfoReader<FileModel>());
                    @SuppressWarnings("unchecked")
                    TableBinder binder = new TableBinder(table, new ListStore(loader) {

                        @Override
                        protected void onLoad(LoadEvent le) {
                            super.onLoad(le);
                            TableEventContext.setSubList((List<FileModel>) this.getModels());
                        }

                    });
                    binder.setAutoSelect(true);
                    binder.addSelectionChangedListener(new SelectionChangedListener<FileModel>() {

                        public void selectionChanged(SelectionChangedEvent<FileModel> se) {
                            List<FileModel> selectFileList = se.getSelection();
                            if (selectFileList != null && selectFileList.size() > 0) {
                                FileModel fileModel = (FileModel) se.getSelection().get(0);
                                ChangeListItemEventContext.attach(fileModel);// 这里设置全局变量
                            } else {
                                ChangeListItemEventContext.attach(null);// 这里设置全局变量
                            }
                        }
                    });
                    loader.load();

                }
            }
        });

    }

    public Table getTable() {
        return table;
    }

}
